package com.niiwoo.civet.mobile.lanmao.handler;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.niiwoo.civet.account.dto.request.CompanyBankCardRebindCallbackRequestDTO;
import com.niiwoo.civet.account.dto.response.CompanyBankCardRebindCallbackResponseDTO;
import com.niiwoo.civet.account.service.bankcard.BankCardDubboService;
import com.niiwoo.tripod.lanmao.enums.LanMaoGatewayServiceNameEnum;
import com.niiwoo.tripod.lanmao.notify.CompanyBindBankcardExpandNotify;
import com.niiwoo.tripod.provider.exception.BizException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 懒猫企业换绑卡回调
 *
 * @author TomXue
 * @since 2018-07-30
 */
@Slf4j
@Component
public class CompanyBindBankcardExpandNotifyHandler implements LanMaoNotifyHandler {

    @Reference(version = "1.0.0")
    private BankCardDubboService bankCardDubboService;

    @Override
    public boolean supports(String serviceName) {
        return LanMaoGatewayServiceNameEnum.ENTERPRISE_BIND_BANKCARD.name().equals(serviceName);
    }

    @Override
    public HandleResult handle(JSONObject json) {
        if (json == null) {
            log.error("企业银行卡换绑回调,请求参数为空.");
        }
        log.info("企业银行卡换绑回调,报文={}", json.toJSONString());
        HandleResult handleResult = new HandleResult();
        handleResult.setNeedAck(false);
        CompanyBindBankcardExpandNotify notify = JSON.parseObject(json.toJSONString(), CompanyBindBankcardExpandNotify.class);
        log.info("企业银行卡换绑回调,请求流水号={},请求参数={}", notify.getRequestNo(), JSON.toJSONString(notify));
        CompanyBankCardRebindCallbackRequestDTO requestDTO = null;
        CompanyBankCardRebindCallbackResponseDTO responseDTO = null;
        //基本调用状态判断
        if (notify.getCode() == 0 && "SUCCESS".equals(notify.getStatus())) {
            requestDTO = new CompanyBankCardRebindCallbackRequestDTO();
            requestDTO.setBankAccountNo(notify.getBankcardNo());
            requestDTO.setBankCode(notify.getBankcode());
            requestDTO.setAccountNo(notify.getPlatformUserNo());
            requestDTO.setRequestNo(notify.getRequestNo());
            responseDTO = bankCardDubboService.companyBankCardRebindCallback(requestDTO);
            handleResult.setRequestNo(responseDTO.getRequestNo());
            handleResult.setData(responseDTO);
            handleResult.setNeedAck(true);
            log.info("企业银行卡换绑回调,请求流水号={},响应结果={}", notify.getRequestNo(), json.toJSONString(handleResult));
            return handleResult;
        } else {
            throw new BizException(notify.getErrorCode(), notify.getErrorMessage());
        }
    }
}
